using System;
using System.Drawing;
using System.Windows.Forms;

namespace ClientTools.Client.Controls
{
    public sealed class GridViewHelper
    {
        public static void SetGridLayout(DataGridView grid )
        {
            if (grid == null) return;

            grid.AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender;
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grid.RowTemplate.Height = 20;
            grid.AllowUserToAddRows = false;
            grid.AllowUserToResizeRows = false;
            grid.AllowUserToResizeColumns = true;
            grid.AllowUserToOrderColumns = true;
        }

        public static void SetDisplayColums(DataGridView grid, String visibleColumns, bool autoSize)
        {
            if (grid != null)
        	{
                DataGridViewColumn dc;

                foreach (DataGridViewColumn var in grid.Columns )
		            var.Visible = false;

                var i = 0;
                
                foreach (var s in visibleColumns.Split(";".ToCharArray()))
	            {
	                if (grid.Columns != null)
	                    if (grid.Columns[s] != null)
	                    {
	                        dc = grid.Columns[s];
	                        if (dc != null)
	                        {
	                            dc.Visible = true;
	                            dc.DisplayIndex = i;
	                        }
	                        i++;
	                        if (autoSize && s != null && grid.Columns[s] != null)
// ReSharper disable PossibleNullReferenceException
	                            grid.Columns[s].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
// ReSharper restore PossibleNullReferenceException
	                    }
	            }

        	    if (grid.Columns != null)
        	        for (int x = 0; x < grid.Columns.Count; x++)
        	            if (grid.Columns[x].DisplayIndex == i-1)
        	            {
        	                grid.Columns[x].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        	                return;
        	            }
        	}
        }

    //Public Overloads Shared Sub SetDisplayColums(ByVal grid As System.Windows.Forms.DataGridView, ByVal VisibleColumns As String, ByVal FillColumn As String)
    //    If Not grid Is Nothing Then

    //        Dim dc As System.Windows.Forms.DataGridViewColumn

    //        For Each dc In grid.Columns
    //            dc.Visible = False
    //        Next

    //        Dim i As Integer
    //        i = 0
    //        With grid.Columns
    //            For Each s As String In VisibleColumns.Split(";".ToCharArray)
    //                If Not .Item(s) Is Nothing Then
    //                    dc = .Item(s)
    //                    dc.Visible = True
    //                    .RemoveAt(.IndexOf(dc))
    //                    .Insert(i, dc)
    //                    i += 1
    //                    dc.AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells
    //                End If
    //            Next
    //        End With
    //        grid.Columns(FillColumn).AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.Fill
    //    End If
    //End Sub
    }
}
